//
//  TopViewController.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/17.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "TopViewController.h"
#import "loadData.h"
#import "TopModal.h"
#import "TopCollectionCell.h"
#import "MovieModal.h"
#import "Header.h"
@interface TopViewController ()
{
    NSMutableArray *_topModalArray;
}
@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.view.backgroundColor = [UIColor purpleColor];
//    
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
//    
//    [self.view addSubview:view];
//    
//    view.backgroundColor = [UIColor blueColor];
    [self initData];
    [self loadData];
    
    [self _creatCollectionView];
    
    
    // Do any additional setup after loading the view.
}

- (void)initData
{
    //避免数据加载多次，每次都创建数组，而降低运行效率
        _topModalArray = [[NSMutableArray alloc]init];
}

#pragma mark - 解析json数据
- (void)loadData
{
//    _movieModalArray = [[NSMutableArray alloc]init];
    NSDictionary *dict = [loadData loadDataWithResourceName:@"top250.json"];
    NSArray *subjects = [dict objectForKey:@"subjects"];
    
    for (NSDictionary *dic in subjects) {
        
        TopModal *topModal = [[TopModal alloc]init];
        
//        topModal.title = [dic objectForKey:@"title"];
////        topModal.year = [dic objectForKey:@"year"];
//        topModal.images = [dic objectForKey:@"images"];
//        
//        float average = [[[dic objectForKey:@"rating"]objectForKey:@"average"]  floatValue];
//        topModal.average = average;
        
        
        
        [topModal setValuesForKeysWithDictionary:dic];
        
        
        [_topModalArray addObject:topModal];
    }
}

#pragma mark -_creatCollectionView
- (void)_creatCollectionView
{
    
    //布局对象，关于collection的布局，统一放到布局对象中去
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
//    layout.minimumLineSpacing = 50;
//    layout.minimumInteritemSpacing = 2;
    
    //设置collectioncell中图片的高度
    layout.itemSize = CGSizeMake((kwidth-40)/3, (kwidth-40)/3*1.5);
    
    NSLog(@"%f,%f",(kwidth-40)/3,(kwidth-40)/3*1.5);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    //    [collection registerNib:<#(UINib *)#> forCellWithReuseIdentifier:<#(NSString *)#>]
    //注册单元项
//    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册nib
    UINib *nib = [UINib nibWithNibName:@"TopCollectionCell" bundle:[NSBundle mainBundle]];
    
    //让tableView来注册nib

    [collectionView registerNib:nib forCellWithReuseIdentifier:@"topCell"];
    
    [self.view addSubview:collectionView];
    
    
}

#pragma mark - cell的相关设置
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _topModalArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topCell" forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor redColor];
    
    //取出数据源里面的数据
   
    
    //把数据源数据传递给cell，让cell来显示，然后就是在TopCollectionCell类里面来完成对cell内容的具体设置
    cell.topModal = _topModalArray[indexPath.row];
    
    return cell;
}


//设置collectionView内部图形在collectionCell内部的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(10, 10, 10, 10);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
